<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login extends MY_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct() {
        parent::__construct();
        $this->load->model('general_model');
    }

    public function verify_login() {

        $this->form_validation->set_rules('username', 'ชื่อผู้ใช้', 'trim|required');
        $this->form_validation->set_rules('password', 'รหัสผ่าน', 'trim|required|callback_check_database');
        $this->form_validation->set_message('required', 'คุณยังไม่ได้กรอก %s !!');

        if ($this->form_validation->run() == FALSE) {
            parent::core_view('body_view');
        } else {
            redirect('login/loged');
        }
    }

    function check_database($password) {

        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $result = $this->general_model->login($username, $password);

        if ($result) {
            $sess_array = array();
            foreach ($result as $row) {
                if ($row->usertype == 2) {
                    $sess_array = array(
                        'usertype' => 'doctor',
                        'user_id' => $row->user_id,
                        'username' => $row->username,
                        'password' => $row->password,
                    );
                } elseif ($row->usertype == 3) {
                    $sess_array = array(
                        'usertype' => 'staff',
                        'user_id' => $row->user_id,
                        'username' => $row->username,
                        'password' => $row->password,
                    );
                } elseif ($row->usertype == 4) {
                    $sess_array = array(
                        'usertype' => 'tech', ##ช่าง
                        'user_id' => $row->user_id,
                        'username' => $row->username,
                        'password' => $row->password,
                    );
                } elseif ($row->usertype == 1) {
                    $sess_array = array(
                        'usertype' => 'admin',
                        'user_id' => $row->user_id,
                        'username' => $row->username,
                        'password' => $row->password,
                    );
                }


                $this->nativesession->set_userdata('logged_in', $sess_array); //เก็บลง Session
            }
        } else {
            $this->form_validation->set_message('check_database', 'ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง ');
            return false;
        }
    }

    public function loged($main_link = false) {
        if ($this->nativesession->userdata('logged_in')) {
            if ($this->usertype == 'admin') {
                redirect('admin');
            } elseif ($this->usertype == 'doctor') {
                redirect('doctor');
            } elseif ($this->usertype == 'staff') {
                redirect(base_url() . 'staff');
            } elseif ($this->usertype == 'tech') {
                redirect(base_url() . 'tech');
            }
        } else {
            redirect('index');
        }
    }

    public function logout() {

        $this->nativesession->sess_destroy();
        redirect(base_url() . '?alert=ออกจากระบบแล้ว"');
        exit();
    }

    //ลงทะเบียน

    public function register($t1 = false, $t2 = false) {

        parent::core_view('member/register');
    }

    public function verify_register() {
        $this->form_validation->set_rules('username', 'ชื่อผู้ใช้', 'trim|required');
        $this->form_validation->set_rules('password', 'รหัสผ่าน', 'trim|required');
        $this->form_validation->set_rules('fname', 'ชื่อ', 'trim|required');
        $this->form_validation->set_rules('lname', 'นาสกุล', 'trim|required');

        $this->form_validation->set_message('required', 'คุณยังไม่ได้กรอก %s !!');


        if ($this->form_validation->run() == FALSE) {
            parent::core_view('member/register');
        } else {
            $site_name = ($this->uri->segment(1) != '') ? $this->uri->segment(1) : '-';
            $tmp = $this->M_Member->_get_site_id($site_name);
            $site_id = $tmp->site_id;
            $data = array(
                'username' => $this->input->post('username'),
                'password' => $this->input->post('password'),
                'fname' => $this->input->post('fname'),
                'lname' => $this->input->post('lname'),
                'site_id' => $site_id,
                'cr_date' => date('Y-m-d h:i:s'),
                'status' => 0,
            );


            $this->general_model->register('site_member', $data);

            $main_link = MAIN_LINK;
            header("location:{$main_link}/member/login?alert=สมัครสมาชิกเรียบร้อยแล้ว");
            exit;
        }
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */